package meterMonitor.service;

import meterMonitor.mapper.DeviceInfoMapper;
import meterMonitor.model.DeviceInfoPO;
import meterMonitor.model.bo.RealtimeDeviceDataBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class DeviceInfoService {

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    public void addDeviceInfo(DeviceInfoPO deviceInfo) {
        deviceInfoMapper.addDeviceInfo(deviceInfo);
    }

    public void updateDeviceInfo(DeviceInfoPO deviceInfo) {
        if (StringUtils.isEmpty(deviceInfo.getPlcAddr())) {
            deviceInfo.setPlcAddr("5000");
        }
        deviceInfoMapper.updateDeviceInfo(deviceInfo);
    }

    public void deleteDeviceInfo(Integer devId) {
        deviceInfoMapper.deleteDeviceInfoById(DeviceInfoPO.newInstance(devId));
    }

    public DeviceInfoPO getDeviceInfoById(Integer devId) {
        return deviceInfoMapper.getDeviceInfoById(devId);
    }

    public List<DeviceInfoPO> queryDeviceInfo(DeviceInfoPO deviceInfo) {
        return deviceInfoMapper.queryDeviceInfo(deviceInfo);
    }

    public List<RealtimeDeviceDataBO> queryRealtimeDeviceDatas(Integer slotId) {
        return deviceInfoMapper.queryRealtimeDeviceDatas(slotId);
    }
}
